#<#
# Resolver Script - This script fixes the root cause. It only runs if the Troubleshooter detects the root cause.
# Key cmdlets: 
# -- get-diaginput invokes an interactions and returns the response
# -- write-diagprogress displays a progress string to the user
##>

param ($id,$name)

#Load functions 
. ./FunctionsLib.ps1  

# RC instance ID (settings Group ID) for which the resolver is running
$Global:RS_Run_ID = $id

# Add the ID to the list of IDs for which resolver already ran
$Global:RS_alreadyRun += $id


Import-LocalizedData -BindingVariable Strings_RS -FileName LocalizationData
write-diagprogress ($Strings_RS.ID_PROG_RESOLVE).Replace("%SettingsGroup%",$name) -Status $Strings_RS.ID_PROG_RESOLVE_STATUS
sleep -Milliseconds 500

if (Test-Path "Settings.xml") 
{  
	# Load XML from disk
	[xml]$xml = Get-Content "Settings.xml" -Force
	
	# Get the settings Group from XML that matches the one that the user selected via interaction  
	$group = $xml.selectSingleNode("/SoftwareReset/SettingsGroup[@id='$id']")
    
	# Processing session group
	if ($group -ne $null)
	 {	   
	# Get a collection of settings to change
	$Settings = $Group.SelectNodes("Settings/*")

	# Iterate over settings and perform each task
	# Also, collect error messages from tasks that are failed and pass it to the troubleshooting report
	$TasksFailed = @()
	foreach ($Setting in $Settings)
	{ 
	    # Determine proper handler for this setting and pass it along
	    switch ($Setting.Name)
	    {  
	        "SetRegValue"   
			{ 	
				Do_SetRegValue $Setting					
				$result = verify_SetRegValue $Setting
				if ($result -ne "fixed")
				{    
					$TasksFailed += $Setting.Name + ": " + $result				
				}
			}
			
	        "DeleteRegValue" 
			{ 
				Do_DeleteRegValue $Setting				
				$result = verify_DeleteRegValue $Setting
				if ($result -ne "fixed")
				{    
					$TasksFailed += $Setting.Name + ": " + $result				
				}
			}
	        "RenameRegValue" 
			{    
				Do_RenameRegValue $Setting
				$result = verify_RenameRegValue $Setting
				if ($result -ne "fixed")
				{    
					$TasksFailed += $Setting.Name + ": " + $result				
				}
			}
	        "CopyFile"       
			{	
				Do_CopyFile $Setting				
				$result = verify_CopyFile $Setting
				if ($result -ne "fixed")
				{    
					$TasksFailed += $Setting.Name + ": " + $result				
				}
			}
	        "DeleteFile"     
			{ 
				Do_DeleteFile $Setting
				$result = verify_DeleteFile $Setting
				if ($result -ne "fixed")
				{    
				 $TasksFailed += $Setting.Name + ": " + $result				
				}
			}
			"Runcommand"     
			{ 
			 	Do_Runcommand $Setting				
			}
	        
	    }		
		
	}

    #pass error messages (if any) to the troubleshooting report
	if ($TasksFailed.length -ne 0)
	{
	 $TasksFailed | convertto-xml | update-diagreport  -id "RC_AppSettings" -name $Strings_RS.ID_NAME_FAILED_TASK -Verbosity Informational  
	}
  }
}
